package com.elink.enginerring.library.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.common.Pagination;
import com.elink.enginerring.library.co.ThesisCo;
import com.elink.enginerring.library.dao.ThesisMapper;
import com.elink.enginerring.library.entity.Thesis;
import com.elink.enginerring.util.IDGenerator;
import com.elink.enginerring.util.JSONUtil;

@Service
public class ThesisService implements IThesisService {
	@Autowired
	private ThesisMapper thesisMapper;

	@Override
	public Map<String, Object> getListOfPage(ThesisCo co) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("list", thesisMapper.selectList(co));
		map.put("pagination", new Pagination(co, thesisMapper.selectCount(co)));

		return JSONUtil.getJSON(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getInfo(String thesisId) {
		return JSONUtil.getJSON(ErrorType.SUCCESS, thesisMapper.selectByThesisId(thesisId));
	}
	
	@Override
	public Map<String, Object> add(Thesis thesis) {
		thesis.setThesisId(IDGenerator.gen32UUID());
		thesis.setUploadTime(new Date());
		thesisMapper.insertSelective(thesis);

		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> update(Thesis thesis) {
		thesisMapper.updateByPrimaryKeySelective(thesis);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> delete(String ids) {
		thesisMapper.delete(ids);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> auth(String ids, int authStatus) {
		thesisMapper.auth(ids,authStatus);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
}
